<template>
  <a-card :bordered="false">
    <div>
      <a-form @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item label="邮箱" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-model="queryParam.email" placeholder="请输入邮箱" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-item>
              <a-button type="primary" icon="search" @click="searchQuery()">查询</a-button>
              <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset()">重置</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <vxe-grid
        id="InviteList"
        ref="vxeTable"
        :row-config="{keyField:'id'}"
        v-bind="options"
        v-on="listeners"
    >
      <template #action="{ row }">
        <a @click="handleDetail(row)">详情</a>
        <a-divider type="vertical" />
        <a-dropdown v-if="row.result&&row.result.summaryReportUrl!=''">
          <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="openReport(row)">打开报告</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="downloadReport(row)">下载报告</a>
            </a-menu-item>
            <!--            <a-menu-item>
                          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(row.id)">
                            <a>删除</a>
                          </a-popconfirm>
                        </a-menu-item>-->
          </a-menu>
        </a-dropdown>

      </template>
    </vxe-grid>
    <InviteModal @ok="modalFormOk" ref="modalForm" />
  </a-card>
</template>

<script>
import { VxeTableMixin } from '@/mixins/VxeTableMixin'
import InviteModal from './modules/InviteModal'
import { downFile } from '@/services/common'
export default {
  name: 'InviteList',
  mixins: [VxeTableMixin],
  components: {
    InviteModal
  },
  data() {
    return {
      columns: [
        {type: 'checkbox', width: 40, fixed: 'left'},
        {type: 'seq', width: 40, fixed: 'left'},
        {title: '邮箱', field: 'email',width: 250},
        {title: '测评活动名称', field: 'activityName',width:300},
        {title: '姓名', field: 'testee.name',width: 200},
        {title: '性别', field: 'testee.textMap.gender$DICT_TEXT_',formatter: 'dictText',width: 100},
        {title: '出生日期', field: 'testee.birthday',width: 150},
        {title: '毕业时间', field: 'testee.graduationDate',width: 150},
        {title: '毕业院校', field: 'testee.graduationSchool',width: 300},
        {title: '专业', field: 'testee.major',width: 200},
        {title: '手机号', field: 'testee.mobile',width: 150},
        {title: '邀请状态', field: 'inviteResult',formatter: 'dictText',width: 150},
        {title: '生效时间', field: 'startTime',width: 150},
        {title: '失效时间', field: 'endTime',width: 150},
        {title: '作答状态', field: 'state',formatter: 'dictText',width: 150},
        {title: '操作', field: 'operation', width: 200, fixed: 'right', slots: {default: 'action'}}
      ],
      url: {
        list: '/assessment/invite/v1/page',
        download: '/assessment/invite/v1/download/'
      },
      customTableToolbar: {
        buttons: [
          /*{
            buttonRender: {
              name: 'VxeToolbarButton',
              events: {click: this.syncActivity},
              props: {title: '同步活动', icon: 'cloud-download'}
            }
          },*/
        ]
      }
    }
  },
  methods: {
    openReport(row){
       window.open(row.result.summaryReportUrl);
    },
    downloadReport(row){
      downFile(this.url.download+row.id).then((data) => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/zip' }), fileName + '.zip')
        } else {
          const url = window.URL.createObjectURL(new Blob([data], { type: 'application/zip' }))
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', row.testee.name+'_'+row.activityName+'_'+row.sn + '.zip')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    }
    /*syncActivity() {
      getAction(this.url.sync).then(res => {
        if (res.success) {
          this.loadData();
        }
      })
    }*/
  }
}
</script>
